package com.health.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Date;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

/**
 * 文件帮助类
 * 
 * @author nova
 *
 */
public class FileHelper {
	private Context context;
	/** SD卡是否存在 **/
	private boolean hasSD = false;
	/** SD卡的路径 **/
	private String SDPATH;
	/** 当前程序包的路径 **/
	private String FILESPATH;

	public FileHelper(Context context) {
		this.context = context;
		hasSD = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
		SDPATH = Environment.getExternalStorageDirectory().getPath()+GlobalConts.FILES_FOLDER_JTYS;
		FILESPATH = this.context.getFilesDir().getPath();
	}

	/**
	 * 在SD卡上创建文件
	 *
	 * @throws IOException
	 */
	public File createSDFile(String fileName) throws IOException {
		File file = new File(SDPATH + "//" + fileName);
		if (!file.exists()) {
			file.createNewFile();
		}
		return file;
	}

	/**
	 * 删除SD卡上的文件
	 *
	 * @param fileName
	 */
	public boolean deleteSDFile(String fileName) {
		File file = new File(SDPATH + "//" + fileName);
		if (file == null || !file.exists() || file.isDirectory())
			return false;
		return file.delete();
	}

	/**
	 * 写入内容到SD卡中的txt文本中 str为内容
	 */
	public boolean writeSDFile(String str, String fileName) {
//		try {
//			FileWriter fw = new FileWriter(SDPATH + "//" + fileName);
//			File f = new File(SDPATH + "//" + fileName);
//			fw.write(str);
//			FileOutputStream os = new FileOutputStream(f,true);//是否追加
//			DataOutputStream out = new DataOutputStream(os);
//			out.writeShort(2);
//			out.writeUTF("");
//			System.out.println(out);
//			os.close();
//			fw.flush();
//			fw.close();
//			System.out.println(fw);
//		} catch (Exception e) {
//		}
		
		try {
			OutputStreamWriter write = null;
			BufferedWriter out = null;
			if (fileName != null) {
				try {   // new FileOutputStream(fileName, true) 第二个参数表示追加写入
					write = new OutputStreamWriter(new FileOutputStream(
							SDPATH + "//" + fileName, true),Charset.forName("gbk"));//一定要使用gbk格式
					out = new BufferedWriter(write, 1024);
				} catch (Exception e) {
				}
			}
            out.write(str);
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {
			Log.e("eeeeeeeeeee", e.getMessage(), e);
			return false;
		}
	}

	/**
	 * 使用FileWriter进行文本内容的追加
	 * @param file
	 * @param content
	 */
	public static void addTxtToFileWrite( String content){
		File file=new File(Environment.getExternalStorageDirectory() + "/NovaJTYS/LogFiles/Log.txt");
		BufferedWriter writer=null;
		String content1=GlobalConts.datetimeFormat1.format(new Date())+":"+content;
		try {
			OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file,true),"gbk");
			writer=new BufferedWriter(write);
			writer.write("\r\n");//换行
			writer.write(content1);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(writer != null){
					writer.flush();
					writer.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


	/**
	 * 读取SD卡中文本文件
	 *
	 * @param fileName
	 * @return
	 */
	public String readSDFile(String fileName) {
		StringBuffer sb = new StringBuffer();
		File file = new File(SDPATH + "//" + fileName);
		try {
			FileInputStream fis = new FileInputStream(file);
			int c;
			while ((c = fis.read()) != -1) {
				sb.append((char) c);
			}
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public String getFILESPATH() {
		return FILESPATH;
	}

	public String getSDPATH() {
		return SDPATH;
	}

	public boolean hasSD() {
		return hasSD;
	}
}